package com.py.gs.util.net.util;

import io.netty.buffer.ByteBuf;

public class Msg {

	public static int writeInt(byte[] bs, int index, int val) {
		bs[index++] = (byte) (val & 0xff);
		bs[index++] = (byte) (val >> 8 & 0xff);
		bs[index++] = (byte) (val >> 16 & 0xff);
		bs[index++] = (byte) (val >> 24 & 0xff);
		return 4;
	}
	
	public static int writeLong(byte[] bs, int index, long val) {
		for (int i = 0; i < 8; i++) {
			bs[index++] = (byte) (val >> (8*i) & 0xff);
		}
		return 8;
	}
	
	public static long readLong(byte[] bs, int index) {
		long rs = 0;
		for (int i = 0; i < 8; i++) {
			rs |= ((bs[index++] & 0xffL) << (i*8));
		}
		return rs;
	}
	
	public static int writeInt24(byte[] bs, int index, int val) {
		bs[index++] = (byte) (val & 0xff);
		bs[index++] = (byte) (val >> 8 & 0xff);
		bs[index++] = (byte) (val >> 16 & 0xff);
		return 3;
	}

	public static int writeShort(byte[] bs, int index, int val) {
		bs[index++] = (byte) (val & 0xff);
		bs[index++] = (byte) (val >> 8 & 0xff);
		return 2;
	}

	public static int readInt(byte[] bs, int index) {
		return (bs[index++] & 0xff) | ((bs[index++] & 0xff) << 8) | ((bs[index++] & 0xff) << 16)
				| ((bs[index++] & 0xff) << 24);
	}
	
	/** 只读取3个字节的int */
	public static int readInt24(byte[] bs, int index) {
		return (bs[index++] & 0xff) | ((bs[index++] & 0xff) << 8) | ((bs[index++] & 0xff) << 16);
	}

	public static int readShort(byte[] bs, int index) {
		return (bs[index++] & 0xff) | ((bs[index++] & 0xff) << 8);
	}

	public static int readInt(ByteBuf in) {
		return (in.readByte() & 0xff) | ((in.readByte() & 0xff) << 8) | ((in.readByte() & 0xff) << 16)
				| ((in.readByte() & 0xff) << 24);
	}
	
	/** 只读取3个字节的int */
	public static int readInt24(ByteBuf in) {
		return (in.readByte() & 0xff) | ((in.readByte() & 0xff) << 8) | ((in.readByte() & 0xff) << 16);
	}

	public static int readShort(ByteBuf in) {
		return (in.readByte() & 0xff) | ((in.readByte() & 0xff) << 8);
	}

	public static int writeByte(byte[] bs, int index, int timeOut) {
		bs[index] = (byte) (timeOut & 0xff);
		return 1;
	}

	
	public static void main(String[] args) {
		byte[] bs = new byte[8];
		writeLong(bs, 0, -5555555555555555555L);
		System.out.println(readLong(bs, 0));
	}


	public static int writeLongStr(byte[] resultCache, int resultPos, long val) {
		if(val == 0) {
			resultCache[resultPos] = '0';
			return 1;
		}
		int stringSize = stringSize(val);
		int i = 0;
		while (val > 0) {
			resultCache[resultPos+stringSize-1-i++] = (byte) (48+(val%10));
			val = val/10;
		}
		return stringSize;
	}

	public static int writeNumStr(byte[] resultCache, int resultPos, int val) {
		if(val == 0) {
			resultCache[resultPos] = '0';
			return 1;
		}
		int stringSize = stringSize(val);
		int i = 0;
		while (val > 0) {
			resultCache[resultPos+stringSize-1-i++] = (byte) (48+(val%10));
			val = val/10;
		}
		return stringSize;
	}
	 
	   // Requires positive x
    static int stringSize(long x) {
        long p = 10;
        for (int i=1; i<19; i++) {
            if (x < p)
                return i;
            p = 10*p;
        }
        return 19;
    }
    
	final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };

	// Requires positive x
	static int stringSize(int x) {
		for (int i = 0;; i++)
			if (x <= sizeTable[i])
				return i + 1;
	}
}
